package com.sc.test;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.sc.test.bean.MessageEvent;
import com.sc.test.utils.RxBus;
import com.sc.test.utils.Util;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Shen Chao on 2018-01-10-010 下午 14:16.
 */

public class EventBusActivity extends AppCompatActivity {
    @BindView(R.id.tv_eventbus_content)
    TextView tvEventbusContent;
    private Disposable disposable;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eventbus);
//        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_content_eventbus, R.id.btn_content_rxbus, R.id.btn_subscribe,
            R.id.btn_content_rxbus2, R.id.btn_content_rxbus3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_content_eventbus:
                Util.openNewActivity(this, SecondActivity.class, "");
                break;
            case R.id.btn_subscribe:
                EventBus.getDefault().register(this);
                break;
            case R.id.btn_content_rxbus:
                Util.openNewActivity(this, SecondActivity.class, "");
                break;
            case R.id.btn_content_rxbus2:
                RxBus.getInstance().toObservable(MessageEvent.class)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<MessageEvent>() {
                            @Override
                            public void accept(MessageEvent messageEvent) throws Exception {
                                tvEventbusContent.setText(messageEvent.getTitle());
                            }
                        });
                break;
            case R.id.btn_content_rxbus3:
                RxBus.getInstance().toObservable(MessageEvent.class)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<MessageEvent>() {
                            @Override
                            public void accept(MessageEvent messageEvent) throws Exception {
                                tvEventbusContent.setText(messageEvent.getName());
                            }
                        });
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        disposable = RxBus.getInstance().toObservable(MessageEvent.class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MessageEvent>() {
                    @Override
                    public void accept(MessageEvent messageEvent) throws Exception {
                        tvEventbusContent.setText("rxjava: " + messageEvent.toString());
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null) {
            disposable.dispose();
        }
        EventBus.getDefault().unregister(this);
    }

    /**
     * 处理事件优先级priority，越高越先执行
     * cancelEventDelivery 可以取消事件，但一定要在发生线程，其他线程无效
     * 黏性事件先发消息后订阅也会生效
     *
     * @param messageEvent
     */
    @Subscribe(threadMode = ThreadMode.POSTING, priority = 100)
    public void onEvent(MessageEvent messageEvent) {
        EventBus.getDefault().cancelEventDelivery(messageEvent);
        Log.i("cancle-------------", "cancle");
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 1)
    public void onEvent2(MessageEvent messageEvent) {
        tvEventbusContent.setText(messageEvent.toString());
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 1, sticky = true)
    public void onEventSticky(MessageEvent messageEvent) {
        tvEventbusContent.append("sticky" + messageEvent.toString());
    }
}
